home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5485 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  66 lines

  1. Newsgroups: comp.lang.c
  2. Path: usenet.eel.ufl.edu!warwick!bsmail!talisker!nathan
  3. From: nathan@pact.srf.ac.uk (Nathan Sidwell)
  4. Subject: Re: Help with pointer alignment
  5. Message-ID: <DMI7Hz.B8C@uns.bris.ac.uk>
  6. Sender: usenet@uns.bris.ac.uk (Usenet news owner)
  7. Nntp-Posting-Host: talisker.pact.srf.ac.uk
  8. Organization: Inmos
  9. X-Newsreader: TIN [version 1.2 PL2]
  10. References: <4fb0op$8l8@hermes.louisville.edu>
  11. Date: Fri, 9 Feb 1996 10:22:47 GMT
  12.  
  13. Alan Wild (arwild01@homer.louisville.edu) wrote:
  14. : I am having a small problem with pointer alignment in a library I am
  15. : developing. lint reports:
  16.  
  17. : "queue.c", line 13: warning: possible pointer alignment problem, op CAST
  18.  
  19. : And the code is as follows:
  20.  
  21. : struct  dataChain {
  22. :         ORD64           dataf;
  23. :         ORD64           be;
  24. :         int             cycle;
  25. : struct  dataChain       *next;
  26. : };
  27.  
  28. : typedef struct dataChain queueentry;
  29.  
  30. : void Append ( queueentry new, queue *Q ) {
  31.  
  32. :         queueentry *temp = (queueentry *)malloc( sizeof( queueentry ) );
  33.  
  34.  
  35. : The line in error is the malloc statement.  What have I done
  36. : wrong/dangerously?  I would like to correct this if at all possible.
  37. : This is the only pententially problematic warning in my code and I would
  38. : like to avoid any problems.  :)
  39.  
  40. assuming that malloc has been declared, the compiler sees that
  41. malloc returns a (char *) or a (void *).
  42.  
  43. A char * has
  44. less strict alignment restrictions than a queueentry * (on your system),
  45. and the compiler sees that your casting the loose pointer to the tight
  46. pointer, possibly causing a problem. The compiler doesn't appear to
  47. know that malloc always returns a correctly aligned pointer.
  48.  
  49. If malloc is declared to return a void *, I think the compiler's
  50. warning unnecessarily, as it should know nothing about the alignment
  51. of the void *.
  52.  
  53. Your code is fine, your compiler is warning unnecessarily. See if
  54. writing it as (queueentry *)(void *)malloc(...) shuts it up.
  55.  
  56. (I've just noticed your talking about lint, not a compiler, but the above
  57. argument still holds, but perhaps it's not possible to stop lint telling
  58. you this.)
  59. nathan
  60.  
  61. --
  62. Nathan Sidwell                         Holder of the Xmris home page
  63. Chameleon Architecture Group at SGS-Thomson, formerly Inmos
  64. http://www.pact.srf.ac.uk/~nathan/                  Tel 0117 9707182
  65. nathan@inmos.co.uk or nathan@bristol.st.com or nathan@pact.srf.ac.uk
  66.